import functools
import time
from base.basepage import BasePage
from common.Message import *


def wait(func):
    @functools.wraps(func)
    def wrapped(self, *args, **kwargs):
        # 在实例方法调用时动态设置等待
        if hasattr(self, 'driver') and self.driver:
            print(WAITING)
            time.sleep(1.5)
            # self.driver.implicitly_wait(5)
            result = func(self, *args, **kwargs)
            time.sleep(1)
            return result
    return wrapped


class Tools(type):
    def __new__(cls, name, bases, attrs):
        for attr_name, attr_value in attrs.items():
            if callable(attr_value) and not attr_name.startswith('__'):
                # 每个方法都增加5秒的等待时间
                attrs[attr_name] = wait(attr_value)
            # elif not attr_name.startswith('__'):
            #     # 每个类属性增加图片目录路径
            #     attrs[attr_name] = f'{BasePage.image_path}/{attr_value}'
        return super().__new__(cls, name, bases, attrs)

